﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SCMS.CoreBusinessLogic.Web;
using SCMS.CoreBusinessLogic.Security;
using SCMS.CoreBusinessLogic.Request4Payment;
using SCMS.UI.Models;
using SCMS.Model;
using SCMS.CoreBusinessLogic.Request4Advance;
using SCMS.CoreBusinessLogic.ActionFilters;
using System.Web.Security;
using SCMS.CoreBusinessLogic.ExpenseClaim;
using SCMS.CoreBusinessLogic.SalaryPayment;

namespace SCMS.UI.Controllers
{
    [MyException]
    public class FundPostingController : PortalBaseController
    {
        private IRequest4Advance rfaService;
        private IRequest4PaymentService rfpService;
        private IExpenseClaimService expenseClaimService;
        private ISalaryPaymentService salaryPaymentService;

        public FundPostingController(IPermissionService permissionService, IUserContext userContext, IRequest4Advance rfaService, IRequest4PaymentService rfpService,
            IExpenseClaimService expenseClaimService, ISalaryPaymentService salaryPaymentService)
            : base(userContext, permissionService)
        {
            this.rfpService = rfpService;
            this.rfaService = rfaService;
            this.expenseClaimService = expenseClaimService;
            this.salaryPaymentService = salaryPaymentService;
        }
        //
        // GET: /FundPosting/

        public ActionResult Index()
        {
            return View();
        }

        //
        // GET: /FundPosting/FundPostingList

        public ActionResult FundPostingList()
        {
            return View();
        }

        //
        // GET: /FundPosting/RFPList

        public ActionResult RFPList()
        {
            List<ViewR4Payment> model = new List<ViewR4Payment>();
            List<PaymentRequest> rfpList = rfpService.GetPaymentRequestsForPosting(countryProg.Id, currentUser);
            foreach (PaymentRequest item in rfpList)
            {
                var p4pobject = new ViewR4Payment()
                {
                    EntityPaymentRqst = item
                };
                model.Add(p4pobject);
            }
            return View(model);
        }

        public ActionResult PostRFP(Guid id)
        {
            ViewBag.RfpId = id;
            ViewBag.Username = currentStaff.Person.OfficialEmail;
            return View();
        }

        public ActionResult PostRFPFunds(Guid id)
        {
            PaymentRequest rfp = rfpService.GetRFPById(id);
            if (rfpService.EffectPosting(rfp, currentStaff))
            {
                rfpService.NotifyAffected(rfp);
                ViewBag.Response = "SUCCESS";
            }
            else
                ViewBag.Response = "FAILURE";
            return View("GeneralResponse");
        }

        //
        // GET: /FundPosting/RFPList

        public ActionResult RFAList()
        {
            List<AdvanceRequestModel> model = new List<AdvanceRequestModel>();
            List<AdvanceRequest> rfaList = rfaService.GetAdvanceRequestsForPosting(countryProg.Id, currentUser);
            foreach (AdvanceRequest item in rfaList)
            {
                model.Add(AdvanceRequestExtension.ToModel(item));
            }
            return View(model);
        }
                
        //
        // GET: /FundPosting/Edit/5

        public ActionResult PostRFA(Guid id)
        {
            ViewBag.RfaId = id;
            ViewBag.Username = currentStaff.Person.OfficialEmail;
            return View();
        }

        public ActionResult PostRFAFunds(Guid id)
        {
            AdvanceRequest rfa = rfaService.GetRFAById(id);
            if (rfaService.EffectPosting(rfa, currentStaff))
            {
                rfaService.NotifyAffected(rfa);
                ViewBag.Response = "SUCCESS";
            }
            else
                ViewBag.Response = "FAILURE";
            return View("GeneralResponse");
        }

        public ActionResult AuthenticatePoster(string password)
        {
            if (Membership.ValidateUser(currentStaff.Person.OfficialEmail, password))
                ViewBag.Response = "SUCCESS";
            else
                ViewBag.Response = "FAILURE";
            return View("GeneralResponse");
        }

        //
        // GET: /FundPosting/RFPList

        public ActionResult ECFList()
        {
            List<ViewExpsClaims> model = new List<ViewExpsClaims>();
            List<ExpenseRequest> expenseClaims = expenseClaimService.GetExpenseClaimsForPosting(countryProg.Id, currentUser);
            foreach (ExpenseRequest item in expenseClaims)
            {
                model.Add(ExpsClaimExtension.PrepareECFModel(expenseClaimService, item.Id));
            }
            return View(model);
        }

        //
        // GET: /FundPosting/Edit/5

        public ActionResult PostECF(Guid id)
        {
            ViewBag.EcfId = id;
            ViewBag.Username = currentStaff.Person.OfficialEmail;
            return View();
        }

        public ActionResult PostECFFunds(Guid id)
        {
            ExpenseRequest ecf = expenseClaimService.GetExpenseClaimById(id);
            if (expenseClaimService.EffectPosting(ecf, currentStaff))
            {
                expenseClaimService.NotifyAffected(ecf);
                ViewBag.Response = "SUCCESS";
            }
            else
                ViewBag.Response = "FAILURE";
            return View("GeneralResponse");
        }

        //
        // GET: /FundPosting/RFPList

        public ActionResult SPMList()
        {
            List<ViewSalaryPayment> model = new List<ViewSalaryPayment>();
            List<SalaryRequest> salaryRequests = salaryPaymentService.GetSalaryPaymentsForPosting(countryProg.Id, currentUser);
            foreach (SalaryRequest item in salaryRequests)
            {
                model.Add(SalaryPaymentExtension.PrepareSP(item.Id));
            }
            return View(model);
        }

        //
        // GET: /FundPosting/Edit/5

        public ActionResult PostSPM(Guid id)
        {
            ViewBag.SpmId = id;
            ViewBag.Username = currentStaff.Person.OfficialEmail;
            return View();
        }

        public ActionResult PostSPMFunds(Guid id)
        {
            SalaryRequest spm = salaryPaymentService.GetSalaryRequestById(id);
            if (salaryPaymentService.EffectPosting(spm, currentStaff))
            {
                //Notify preparer and Project Manager(s) that posting has been done
                salaryPaymentService.NotifyAffected(spm);
                ViewBag.Response = "SUCCESS";
            }
            else
                ViewBag.Response = "FAILURE";
            return View("GeneralResponse");
        }
    }
}
